﻿using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class DeviceInfo : MonoBehaviour
    {
        private const uint kNumFrameTimings = 2;
        public Text text;
        public Camera[] list;
        public Camera current;
        private readonly FrameTiming[] frameTimings = new FrameTiming[3];

        private Camera camera
        {
            get
            {
                if (current != null && current.gameObject.activeSelf) return current;
                for (var i = 0; i < list.Length; i++)
                    if (list[i].gameObject.activeSelf)
                    {
                        current = list[i];
                        break;
                    }

                return current;
            }
        }

        private void Update()
        {
            FrameTimingManager.CaptureFrameTimings();
            FrameTimingManager.GetLatestTimings(kNumFrameTimings, frameTimings);
            if (frameTimings.Length < kNumFrameTimings) return;

            var gpu = frameTimings[0].gpuFrameTime;
            var cpu = frameTimings[0].cpuFrameTime;
            text.text = string.Format(
                "device: {0}; platform: {1}; devicePlatform: {2}; canvas: {3}; screen: {4}; resolution: {5}; current: {6}; camera: {7}; scaleCamera: {8}; Quality: {9}; FrameRate: {10}; FPS: {11}; CPU: {12}; GPU: {13};",
                Game.GetDevice(), Game.GetPlatform(), Game.GetDevicePlatform(), SystemInput.canvasSize,
                SystemInput.screenSize,
                SystemInput.resolution, Screen.currentResolution, new Vector2(camera.pixelWidth, camera.pixelHeight),
                new Vector2(camera.scaledPixelWidth, camera.scaledPixelHeight),
                QualitySettings.GetQualityLevel(), Application.targetFrameRate, (int)(1 / Time.deltaTime), (int)cpu,
                (int)gpu);
        }
    }
}